/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package daw.controlador;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Luis
 */
public class ReporteDenuncias extends HttpServlet {
   	@PersistenceUnit
    private EntityManagerFactory emf;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
		String operacion;
        try {
			operacion = request.getParameter("operacion");
			if(operacion == null){
				throw new ServletException("No se ha definido valor de operación.");
			}else if(operacion.equals("prepararParaConsultar")){
				getServletContext().getRequestDispatcher("/servicio/protegido/reporteDenuncias.jsp").forward(request, response);
			}else if(operacion.equals("consultar")){
				EntityManager em;
				List denuncias;
				Date fechaInicio= null;
				Date fechaFin= null;
				java.util.Date fecha;
				int year = Integer.parseInt(request.getParameter("year"));
				int trimestre = Integer.parseInt(request.getParameter("trimestre"));
				int mesInicio;
				int mesFin;
				int diaInicio;
				int diaFin;
				diaInicio = 1;
				switch(trimestre){
					case 1:
						mesInicio = 1;
						mesFin = 3;
						diaFin = 31;
						break;
					case 2:
						mesInicio = 4;
						mesFin = 6;
						diaFin = 30;
						break;
					case 3:
						mesInicio = 7;
						mesFin = 9;
						diaFin = 30;
						break;
					default:
						mesInicio = 10;
						mesFin = 12;
						diaFin = 31;
						break;
				}
				
				fechaInicio = new GregorianCalendar(year, mesInicio, diaInicio).getTime();
				fechaFin = new GregorianCalendar(year, mesFin, diaFin).getTime();

				em = emf.createEntityManager();
				denuncias = em.createNamedQuery("Denuncia.findByRangoFechaDelito").setParameter("fechaDelitoInicio", fechaInicio).setParameter("fechaDelitoFin", fechaFin).getResultList();
				em.close();
				request.setAttribute("denuncias", denuncias);
				getServletContext().getRequestDispatcher("/servicio/protegido/resultadoConsulta.jsp").forward(request, response);
			}
        }catch(Exception e){
			e.printStackTrace(response.getWriter());
		}finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
